package com.dal.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import com.dal.db.bean.TempReward;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import com.sojoys.artifact.manager.JsonManager;
import java.sql.SQLException;
import com.google.common.collect.Lists;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import static com.dal.db.StaticResource.TempRewardResource.*;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static com.dal.db.StaticResource.TempRewardResource.*;
/**
 * 临时奖励
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TempRewardDaoImpl implements BaseDao<String,TempReward>{
	static Log log = LogFactory.get(TempRewardDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private TempRewardDaoImpl() {}
	  
    private static final TempRewardDaoImpl INSTANCE = new TempRewardDaoImpl();  
    
    public static TempRewardDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public TempReward insert(TempReward tempReward){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(tempReward,conn);
	}
	
	public TempReward insert(TempReward tempReward,Connection conn){
		return insert(tempReward,conn,TABLE_NAME);
	}
	
	public TempReward insert(TempReward tempReward,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(tempReward,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public TempReward insert(TempReward tempReward,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(tempReward,conn,tableName);
	}
	
	public TempReward insert(TempReward tempReward,Connection conn,String tableName){
		
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (phoneNumber,item_id,item_count,receive,create_date,modified_date,receive_date) VALUES (?,?,?,?,?,?,?)";
		try {
			int i = run.update(conn,sql,tempReward.getPhoneNumber(),tempReward.getItemId(),tempReward.getItemCount(),tempReward.isReceive(),tempReward.getCreateDate(),tempReward.getModifiedDate(),tempReward.getReceiveDate());
			return i > 0 ? tempReward : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public TempReward insert(TempReward tempReward,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(tempReward,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<TempReward> tempRewards){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(tempRewards,conn);
	}
	
	public int[] insertBatch(List<TempReward> tempRewards,Connection conn){
		return insertBatch(tempRewards,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<TempReward> tempRewards,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(tempRewards,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<TempReward> tempRewards,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(tempRewards,conn,tableName);
	}
	
	public int[] insertBatch(List<TempReward> tempRewards,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (phoneNumber,item_id,item_count,receive,create_date,modified_date,receive_date) VALUES (?,?,?,?,?,?,?)";
		try {
			int columnSize = 7;
			int size = tempRewards.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =tempRewards.get(i).getPhoneNumber();
				params[i][1] =tempRewards.get(i).getItemId();
				params[i][2] =tempRewards.get(i).getItemCount();
				params[i][3] =tempRewards.get(i).isReceive();
				params[i][4] =tempRewards.get(i).getCreateDate();
				params[i][5] =tempRewards.get(i).getModifiedDate();
				params[i][6] =tempRewards.get(i).getReceiveDate();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<TempReward> tempRewards,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(tempRewards,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public TempReward update(TempReward tempReward){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(tempReward,conn);
	}
	
	public TempReward update(TempReward tempReward,Connection conn){
		return update(tempReward,conn,TABLE_NAME);
	}
	
	public TempReward update(TempReward tempReward,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(tempReward,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public TempReward update(TempReward tempReward,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(tempReward,conn,tableName);
	}
	
	public TempReward update(TempReward tempReward,Connection conn,String tableName){
		List<TempReward> tempRewards = new ArrayList<TempReward>();
		tempRewards.add(tempReward);
		updateBatch(tempRewards,conn,tableName);
		return tempReward;
	}
	
	public TempReward update(TempReward tempReward,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(tempReward,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<TempReward> tempRewards){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(tempRewards,conn);
	}
	
	public int[] updateBatch(List<TempReward> tempRewards,Connection conn){
		return updateBatch(tempRewards,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<TempReward> tempRewards,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(tempRewards,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<TempReward> tempRewards,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(tempRewards,conn,tableName);
	}
	
	public int[] updateBatch(List<TempReward> tempRewards,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET phoneNumber = ?,item_id = ?,item_count = ?,receive = ?,create_date = ?,modified_date = ?,receive_date = ? WHERE phoneNumber = ?";
		try {
			int columnSize = 7;
			int size = tempRewards.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =tempRewards.get(i).getPhoneNumber();
				params[i][1] =tempRewards.get(i).getItemId();
				params[i][2] =tempRewards.get(i).getItemCount();
				params[i][3] =tempRewards.get(i).isReceive();
				params[i][4] =tempRewards.get(i).getCreateDate();
				params[i][5] =tempRewards.get(i).getModifiedDate();
				params[i][6] =tempRewards.get(i).getReceiveDate();
				params[i][columnSize] =tempRewards.get(i).getPhoneNumber();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<TempReward> tempRewards,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(tempRewards,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(TempReward tempReward){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(tempReward,conn);
	}
	
	public boolean delete(TempReward tempReward,Connection conn){
		return delete(tempReward,conn,TABLE_NAME);
	}
	
	public boolean delete(TempReward tempReward,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(tempReward,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(TempReward tempReward,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(tempReward,conn,tableName);
	}
	
	public boolean delete(TempReward tempReward,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE phoneNumber = ?";
		try {
			int i = run.update(conn,sql, tempReward.getPhoneNumber());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(TempReward tempReward,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(tempReward,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<TempReward> tempRewards){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(tempRewards,conn);
	}
	
	public boolean deleteBatch(List<TempReward> tempRewards,Connection conn){
		return deleteBatch(tempRewards,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<TempReward> tempRewards,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(tempRewards,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<TempReward> tempRewards,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(tempRewards,conn,tableName);
	}
	
	public boolean deleteBatch(List<TempReward> tempRewards,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE phoneNumber = ?";
		try {
			int size = tempRewards.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = tempRewards.get(i).getPhoneNumber();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<TempReward> tempRewards,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(tempRewards,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( phoneNumber ) 查询
	 */
	public TempReward getByPhoneNumber(String phoneNumber){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPhoneNumber(phoneNumber, conn);
	}
	
	public TempReward getByPhoneNumber(String phoneNumber,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPhoneNumber(phoneNumber, conn,tableName);
	}
	public List<TempReward> getIn(Set<String> phoneNumbers){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(phoneNumbers, conn);
	}
	
	public List<TempReward> getIn(Set<String> phoneNumbers,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(phoneNumbers, conn,tableName);
	}
	
	//Connection
	/**
	 * 根据( phoneNumber ) 查询
	 */
	public TempReward getByPhoneNumber(String phoneNumber,Connection conn){
		return getByPhoneNumber(phoneNumber,conn,TABLE_NAME);
	}
	
	public TempReward getByPhoneNumber(String phoneNumber,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT phoneNumber,item_id,item_count,receive,create_date,modified_date,receive_date FROM " + tableName + " WHERE " + "phoneNumber = ? ORDER BY phoneNumber ASC LIMIT 1";
		TempReward tempReward = null; 
		try {
			tempReward = run.query(conn,sql, new TempRewardHandler(), phoneNumber);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return tempReward;
	}
	public List<TempReward> getIn(Set<String> phoneNumbers,Connection conn){
		return getIn(phoneNumbers,conn,TABLE_NAME);
	}
	
	public List<TempReward> getIn(Set<String> phoneNumbers,Connection conn,String tableName){
		List<TempReward> tempRewards = Lists.newArrayList();
		if(phoneNumbers.isEmpty()) return tempRewards;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT phoneNumber,item_id,item_count,receive,create_date,modified_date,receive_date FROM " + tableName + " WHERE " + "phoneNumber in (%s) ORDER BY phoneNumber ASC";
		sql = String.format(sql, Joiner.on(",").join(phoneNumbers));
		try {
			tempRewards = run.query(conn,sql, new TempRewardListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return tempRewards;
	}
	
	//DataSource
	/**
	 * 根据( phoneNumber ) 查询
	 */
	public TempReward getByPhoneNumber(String phoneNumber,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByPhoneNumber(phoneNumber, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public TempReward getByPhoneNumber(String phoneNumber,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByPhoneNumber(phoneNumber, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<TempReward> getIn(Set<String> phoneNumbers,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(phoneNumbers, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<TempReward> getIn(Set<String> phoneNumbers,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(phoneNumbers, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<TempReward> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<TempReward> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<TempReward> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<TempReward> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<TempReward> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT phoneNumber,item_id,item_count,receive,create_date,modified_date,receive_date FROM " + tableName + " ORDER BY phoneNumber ASC";
		List<TempReward> tempRewards = Lists.newArrayList();
		try {
			tempRewards = run.query(conn, sql, new TempRewardListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return tempRewards;
	}
	
	public List<TempReward> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<TempReward> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<TempReward> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<TempReward> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<TempReward> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<TempReward> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT phoneNumber,item_id,item_count,receive,create_date,modified_date,receive_date FROM " + tableName + " ORDER BY phoneNumber ASC LIMIT " + page + " , " +pageSize;
		List<TempReward> tempRewards = Lists.newArrayList();
		try {
			tempRewards = run.query(conn, sql, new TempRewardListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return tempRewards;
	}
	
	public List<TempReward> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `phoneNumber` VARCHAR(50) NOT NULL  COMMENT 'remarks=手机号',");	
		plus.a("  `item_id` INT(11)   COMMENT 'remarks=道具ID',");	
		plus.a("  `item_count` INT(11)   COMMENT 'remarks=道具数量',");	
		plus.a("  `receive` TINYINT(1)   COMMENT 'remarks=是否领取',");	
		plus.a("  `create_date` DATETIME   COMMENT 'remarks=创建时间',");
		plus.a("  `modified_date` DATETIME   COMMENT 'remarks=修改时间',");
		plus.a("  `receive_date` DATETIME   COMMENT 'remarks=领取时间',");
		plus.a("  PRIMARY KEY (`phoneNumber`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8  COMMENT='remarks=临时奖励';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"phoneNumber\",\"NULLABLE\":0,\"REMARKS\":\"remarks=手机号\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":150,\"ORDINAL_POSITION\":1,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":50}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"item_id\",\"NULLABLE\":1,\"REMARKS\":\"remarks=道具ID\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":2,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"item_count\",\"NULLABLE\":1,\"REMARKS\":\"remarks=道具数量\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":3,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"receive\",\"NULLABLE\":1,\"REMARKS\":\"remarks=是否领取\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":4,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":-6,\"TYPE_NAME\":\"TINYINT\",\"COLUMN_SIZE\":1}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":1,\"REMARKS\":\"remarks=创建时间\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":5,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":1,\"REMARKS\":\"remarks=修改时间\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":6,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"SCOPE_TABLE\":null,\"TABLE_CAT\":\"dal_login_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"temp_reward\",\"COLUMN_DEF\":null,\"SCOPE_CATALOG\":null,\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"receive_date\",\"NULLABLE\":1,\"REMARKS\":\"remarks=领取时间\",\"DECIMAL_DIGITS\":null,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":null,\"ORDINAL_POSITION\":7,\"SCOPE_SCHEMA\":null,\"SOURCE_DATA_TYPE\":null,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_login_design\",\"PAGES\":null,\"TABLE_NAME\":\"temp_reward\",\"TABLE_SCHEM\":null,\"COLUMN_NAME\":\"phoneNumber\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_login_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"FILTER_CONDITION\":null,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class TempRewardHandler implements ResultSetHandler<TempReward>{
	@Override
	public TempReward handle(ResultSet rs) throws SQLException {
		return rs.next() ? TempReward.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class TempRewardListHandler extends AbstractListHandler<TempReward>{
	@Override
	protected TempReward handleRow(ResultSet rs) throws SQLException {
		return TempReward.builder().createForResultSet(rs).syncIndexValues();
	}
}

//自定义内容结束位置
}

